﻿using System.Activities.Presentation;
using System.Collections.Generic;
using System.Windows.Controls;

namespace Vision.VisionWorkshop.Controls
{
    /// <summary>
    /// 
    /// </summary>
    public partial class TypeBrowserControl : UserControl
    {
        private WorkflowDesigner wd { get; set; }

        public TypeBrowserControl(WorkflowDesigner wd)
        {
            this.wd = wd;

            InitializeComponent();
            SetContent();
        }

        private void RefreshButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SetContent();
        }

        private void SetContent()
        {
            // Get type data from workflow
            IList<TypeInfo> Members = TypeDataSource.GenerateTypeInfoList(this.wd);

            // Create UI-friendly wrappers around the 
            // raw data objects and bind to the UI
            base.DataContext = new TypeBrowserViewModel(Members);
        }

        private void SelectedItemChangedHandler(object sender, System.Windows.RoutedPropertyChangedEventArgs<object> e)
        {
            var Item = e.NewValue as TypeViewModel;
            string FullyQualifiedName = string.Empty;

            if (Item != null)
            {
                FullyQualifiedName = GetFullyQualifiedName(Item);
            }
            this.SelectedType.Text = FullyQualifiedName;
        }

        private string GetFullyQualifiedName(TypeViewModel Item)
        {
            if (Item.Parent != null)
            {
                return GetFullyQualifiedName(Item.Parent) + "." + Item.MemberName;
            }
            else
            {
                return Item.MemberName;
            }
        }
    }
}